package com.example.xui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.example.xui.Main.FourFragment;
import com.example.xui.Way.changStatusIconCollor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Picture_Selection extends AppCompatActivity {
     private FourFragment myFragment;
    static int i=R.drawable.toxiang;
    int[] imageIds = new int[]
            {
                    R.drawable.toxiang,R.drawable.toxiang2,R.drawable.toxiang3,R.drawable.toxiang4,R.drawable.toxiang5,R.drawable.toxiang6,
                    R.drawable.toxiang7,R.drawable.toxiang8,R.drawable.toxiang9,R.drawable.toxiang10,R.drawable.toxiang11,R.drawable.toxiang12
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture__selection);
        changStatusIconCollor changStatusIconCollor=new changStatusIconCollor();
        changStatusIconCollor.changStatusIconCollor1(getWindow(),true);
      /*  Intent intent=new Intent(MainActivity.this,Main2Activity.class);
        intent.putExtra()
        startActivity(intent);*/
        //创建一个list对象，list对象的元素是map
        List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>();

        for (int i = 0; i < imageIds.length; i++) {
            Map<String, Object> listItem = new HashMap<String, Object>();
            listItem.put("image", imageIds[i]);
            listItems.add(listItem);
        }
        //获取显示图片的ImageSwitcher
        final ImageSwitcher switcher = (ImageSwitcher) findViewById(R.id.switcher);
        //设置图片更换的动画效果
        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        //为ImageSwitcher设置图片切换的动画效果
        switcher.setFactory(new ViewSwitcher.ViewFactory() {

            @Override
            public View makeView() {
                ImageView imageView = new ImageView(Picture_Selection.this);
                imageView.setBackgroundColor(0xff0000);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                return imageView;
            }
        });

        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});

        GridView grid = (GridView) findViewById(R.id.grid01);
        //为GridView设置Adapter
        grid.setAdapter(simpleAdapter);
        //添加列表项被选中的监听器
        grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position , long id) {
                //显示当前被选中的图片
                switcher.setImageResource(imageIds[position % imageIds.length]);
                i=imageIds[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {}
        });
        //添加列表被单击的监听
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                switcher.setImageResource(imageIds[position % imageIds.length]);
                i=imageIds[position];
               // Toast.makeText(MainActivity.this,"i="+i,Toast.LENGTH_LONG).show();
            }

        });
    /*  *//*  Bundle bundle=new Bundle*//*();
        bundle.put*/
        ImageView button=findViewById(R.id.imageView2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent intent=new Intent(Picture_Selection.this,LoardActivity.class);
                intent.putExtra("toxiang",i);
                intent.putExtra("tag",4);
                startActivity(intent);*/
                Bundle bundle=new Bundle();
                bundle.putInt("toxiang",i);
                myFragment.setArguments(bundle);
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                myFragment=new FourFragment();
                transaction.replace(R.id.tt,myFragment);
                transaction.commit();
              /*  myFragment.setArguments(bundle);
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                myFragment=new FourFragment();
                transaction.replace(R.id.tt,myFragment);
                transaction.commit();*/
            }
        });

    }
}
